package com.example.ymm.bo;


import com.example.ymm.util.UtilValidate;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class CacheMapService {

    //用户缓存
    private ConcurrentHashMap<String, YmUserDetails> userMap=new ConcurrentHashMap<>();

    //普通数据缓存
    private ConcurrentHashMap<String,Object> strMap=new ConcurrentHashMap<>();

    //普通数据缓存
    private ConcurrentHashMap<String, AtomicInteger> intKeys=new ConcurrentHashMap<>();


    public YmUserDetails getUserLogin(String userId) {
        return userMap.get(userId);
    }

    public void setUserLogin(String userId, YmUserDetails user) {
        this.userMap.put(userId,user);
    }

    public void removeUserLogin(String userId) {
        this.userMap.remove(userId);

    }

    public int autoIncrement(String key){
        AtomicInteger atomicInteger = intKeys.get(key);
        if(UtilValidate.isEmpty(atomicInteger)){
            atomicInteger=new AtomicInteger();
            intKeys.put(key,atomicInteger);
        }
        return atomicInteger.incrementAndGet();
    }

    public Object getStrMap(String key) {
        return strMap.get(key);
    }

    public void setStrMap(String key,Object obj) {
        this.strMap.put(key, obj);
    }

    public void removeStrMap(String key) {
        strMap.remove(key);
    }
}
